import { observable, action } from 'mobx';
import { Toast } from 'antd-mobile';

class Account {
	
	// 彪锅数据
	@observable data = {
		total: 0,
		categories: [],
		table_lists:['1号', '2号', '3号', '4号', '5号', '6号', '7号', '8号'],
		isGoOrder: false,
	};

	// 获取分类
	@action getFetch() {
		let that = this;
		fetch('http://zongwu.top/biaoguo-api/categories/', {
			method: 'GET',
		}).then(function(response) {
			return response.json();
		}).then(function(data) {
			console.log(data);
			that.data.categories = data.map((d) => {
				return Object.assign({}, {count: 0}, d);
			})

		}).catch(function(err) {
			console.log(err);
		})
	}

	// 提交订单总价
	@action async fetchTotal() {
		let that = this;
		console.log(this.data.total);
		if ( that.data.total != 0 ) {
			await fetch("http://zongwu.top/biaoguo-api/total_price/",{
			  method:'POST',
			  headers:{
			     'Content-Type': 'application/json'
			  },
			  body:JSON.stringify({
			    total: that.data.total,
			  })
			}).then((response)=>response.json())
			.then((responseJsonData)=>{
			  console.log(responseJsonData);
			  if(responseJsonData.id) {
				Toast.success('成功', 1);
				that.data.isGoOrder = true;
			  }
			})
			.catch((error)=>{
			  alert(error);
			})
		}else {
			Toast.fail('没有选择菜单', 1);
		}
		
	}
}

const account = new Account();
export default account;